package com.hongye.broker.controller;


import com.hongye.broker.service.StoreyService;
import com.hongye.common.entity.StoreyEntity;
import com.hongye.common.response.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("storey")
public class StoreyController {

    @Autowired
    private StoreyService storeyService;


    //楼市圈发布列表接口
    @GetMapping("findAll")
    public ResponseResult findAll(){
        List<StoreyEntity> list  = storeyService.findAll();
        /*String s = JSON.toJSONString(list);
        StoreyEntity storeyEntity = JSONArray.parseObject(s, StoreyEntity.class);
        //Map<Long, StoreyEntity> collect = list.stream().collect(Collectors.toMap(StoreyEntity::getId, Function.identity()));*/
        Map<String, Object> map = new HashMap<>();
        map.put("list",list);

        return new ResponseResult(200,"查询成功",map);
    }

    //删除接口
    @RequestMapping("delete")
    public ResponseResult dele(Integer id){
        try {
            storeyService.dele(id);
            return new ResponseResult(200,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(201,"删除失败");
        }
    }

    //发布接口
    @RequestMapping("insert")
    public ResponseResult save(@RequestBody StoreyEntity storeyEntity){
        try {
            storeyService.save(storeyEntity);
            return new ResponseResult(200,"发布成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(201,"发布失败");
        }
    }
}
